<?php

// +----------------------------------------------------------------------

// | Fanwe 多语商城建站系统 (Build on ThinkPHP)

// +----------------------------------------------------------------------

// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.

// +----------------------------------------------------------------------

// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )

// +----------------------------------------------------------------------

// | Author: 云淡风轻(97139915@qq.com)

// +----------------------------------------------------------------------



// 商品分类

class GoodsCateAction extends CommonAction{

	//列表

	public function index() {

		$list = D(MODULE_NAME) -> findAll();

		$list = D(MODULE_NAME) -> toFormatTree($list,array('name'));

		$this->assign("list",$list);

		$this->display ();

		return;

	}

	//增

	public function add()
	{

		$list = D(MODULE_NAME)-> where("pid = 0")-> findAll();

		$list = D(MODULE_NAME)-> toFormatTree($list,array('name'));

		$new_sort = D(MODULE_NAME) -> max("sort") + 1;

		$this->assign('new_sort',$new_sort);

		$this->assign("cate_list",$list);

		$this->display();

	}

	//改

	public function edit()

	{

		//开始获取当前分类与其子分类的ID

		$pk = D(MODULE_NAME)->getPk();

		$list = D(MODULE_NAME)-> where("pid = 0")-> findAll();

		$list = D(MODULE_NAME)-> toFormatTree($list,array('name'));



		$this->assign("cate_list",$list);



		parent::edit();

	}





	public function insert() {

		$is_default = intval($_REQUEST['is_default']);

		if($is_default == 1)

		{

			D(MODULE_NAME)->setField("is_default",0);

		}



		$name=$this->getActionName();

		$model = D ($name);

		$data  = $model->create ();

		if (false === $data) {

			$this->error ( $model->getError () );

		}





		//保存当前数据对象

		$list=$model->add ($data);

		if ($list!==false) { //保存成功

			$msg = '添加团购分类'.$data['name'];

			$this->saveLog(1,$list,$msg);



//			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );

			$this->success (L('ADD_SUCCESS'));

		} else {

			//失败提示

			$msg = '添加团购分类'.$data['name'];

			$this->saveLog(0,0,$msg);

			$this->error (L('ADD_FAILED'));

		}

	}



	public function update() {

		$is_default = intval($_REQUEST['is_default']);

		if($is_default == 1)

		{

			D(MODULE_NAME)->setField("is_default",0);

		}



		$name=$this->getActionName();

		$model = D ( $name );

		$data = $model->create ();

		if (false === $data) {

			$this->error ( $model->getError () );

		}

		// 更新数据

		$list=$model->save ($data);

		if (false !== $list) {

			//成功提示

			$msg = '修改团购分类'.$data['name'];

			$this->saveLog(1,$list,$msg);

//			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );

			$this->success (L('EDIT_SUCCESS'));

		} else {

			//错误提示

			$msg = '修改团购分类'.$data['name'];

			$this->saveLog(0,0,$msg);

			$this->error (L('EDIT_FAILED'));

		}

	}



	//删

	public function foreverdelete() {

		//删除指定记录

		$name=$this->getActionName();

		$model = D ($name);

		if (! empty ( $model )) {

			$pk = $model->getPk ();

			$id = $_REQUEST [$pk];

			$ids = explode ( ',', $id );

			$names = '';

			foreach($ids as $idd)

			{

				$names .= M("GoodsCate")->where("id=".$idd)->getField("name").",";

			}

			if($names!='')

			{

				$names = substr($names,0,strlen($names)-1);

			}

			if (isset ( $id )) {

				//进行分类删除的验证
                $condition = array ('pid' => array ('in',$ids));
				if($model->where($condition)->count() > 0)
                    $this->error(L('CHILD_CATE_EXIST'));

                $condition = array ('cate_id' => array ('in',$ids));
				if(D("GoodsKey")->where($condition)->count() > 0)
                    $this->error(L('GOODS_EXIST'));

				//验证结束

				$condition = array($pk => array ('in',$ids));

				if (false !== $model->where ( $condition )->delete ()) {

					//echo $model->getlastsql();

					$msg = '删除团购分类:'.$names."ID:".$id;

					$this->saveLog(1,0,$msg);



					$this->success (L('DEL_SUCCESS'));

				} else {

					$msg = '删除团购分类:'.$names."ID:".$id;

					$this->saveLog(0,0,$msg);

					$this->error (L('DEL_FAILED'));

				}

			} else {

				$msg = '删除团购分类:'.$names;

				$this->saveLog(0,0,$msg);

				$this->error ( L('INVALID_OP') );

			}

		}

		$this->forward ();

	}

}

?>